package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileLaunchEvent;

import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class BanEnderPearlFlag extends JavaLandFlag implements Listener{
	private String tip;
	
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.MUCH;
	}
	@Override
	public ELandFlagType getELandFlagType() {
		return ELandFlagType.PLAYER;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止使用末影珍珠");
			config.set("lore", Arrays.asList("&7（禁止其它玩家在领地内使用末影珍珠）","&7（末影珍珠将无法在领地内使用）","&5（标记参数为许可玩家）"));
			config.set("tip", "&c没有领地许可，不可以使用末影珍珠");
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.tip = LanguageManager.getChatPrefix() + Util.conver(config.getString("tip"));
	}
	
	@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
	public void ProjectileLaunch(final ProjectileLaunchEvent e) {
		if(!(e.getEntity() instanceof EnderPearl)) return;
		final EnderPearl ender_pearl = (EnderPearl) e.getEntity();
		if(!(ender_pearl.getShooter() instanceof Player)) return;
		final Land land = ILandManager.getILandManager().getHighestPriorityLand(e.getEntity().getLocation());
		if(land == null) return;
		final Player player = (Player) ender_pearl.getShooter();
		if(JavaLandFlag.hasLandFlagParameterAllMethod(land, this, player)) return;
		e.setCancelled(true);
		player.sendMessage(this.tip);
	}
}